package com.atguigu.ditu3.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.atguigu.ditu3.GPSinfoDao;
import com.atguigu.ditu3.GpsInfo;
import com.atguigu.ditu3.MainActivity;
import com.atguigu.ditu3.R;
import com.atguigu.ditu3.activity.adapter.haoyouadapter;
import com.atguigu.ditu3.activity.bean.ContactBean;
import com.atguigu.ditu3.activity.bean.dingwei;
import com.atguigu.ditu3.utils.Constant;
import com.atguigu.ditu3.utils.MyUtils;
import com.atguigu.ditu3.utils.SpUtils;
import com.atguigu.ditu3.utils.zhuiguiji;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.google.gson.Gson;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.OnResponseListener;
import com.yolanda.nohttp.Request;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.RequestQueue;
import com.yolanda.nohttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class getmanydingwei extends Activity {
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private List<ziti> zuohao;
    private List<dingwei>  dwlt;
    private ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_getmanydingwei);
        mMapView= (MapView) findViewById(R.id.id_bmapView);
        list= (ListView) findViewById(R.id.list);
        mBaiduMap=mMapView.getMap();
        Button back= (Button) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(18.0f);
        mBaiduMap.setMapStatus(msu);




        ContactBean bean=new ContactBean();
        String userphone = SpUtils.getStringValue(getmanydingwei.this, "userphone", "");
        bean.phone=userphone;
        getlist_3(bean, Constant.GET_MANY_DINGWEI);





                   //弹窗要展示的信息实体
                 //  Fire fire = new Fire();
//                   //获取之前传递的Fire
//                 String  fire = (String) marker.getExtraInfo()
//                           .getSerializable("Fire");

                   //mBaiduMap.hideInfoWindow();
//
//                   //渲染布局模版
//                   MarkerFire markerFire = new MarkerFire(
//                           getActivity(), fire);
//
//                   BitmapDescriptor descriptor = BitmapDescriptorFactory
//                           .fromView(markerFire);
//                   //获取标注坐标显示对应弹窗
//                   LatLng ll = marker.getPosition();
//                   InfoWindow mInfoWindow = new InfoWindow(null, ll, -47, listener);
//                   mBaiduMap.showInfoWindow(mInfoWindow);






    }
   private class  adapt extends BaseAdapter{
       private final Context context;
       private final List<ziti> lis;

       public adapt(List<ziti> st, Context mContxet){
           this.lis=st;
           this.context=mContxet;
       }
       @Override
       public int getCount() {
           if(lis!=null){
               return lis.size();
           }
           return 0;
       }

       @Override
       public Object getItem(int position) {
           return null;
       }

       @Override
       public long getItemId(int position) {
           return 0;
       }

       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
           View v = null;
           TextView data;
           if(convertView==null){
               v=View.inflate(getApplication(),R.layout.item_history,null);
               data=(TextView) v.findViewById(R.id.tv_date);

               v.setTag(data);
           }else{
               v=convertView;
               data= (TextView) v.getTag();
           }
           data.setText(lis.get(position).mingzi);
           return v;
       }
   }


    private void getlist_3(Object object, String url) {
        RequestQueue queue = NoHttp.newRequestQueue();
        Request<String> request = NoHttp.createStringRequest(url, RequestMethod.POST);
        Gson gson = new Gson();
        String json = gson.toJson(object);
        request.add("lrs", json);
        queue.add(88, request, new OnResponseListener<String>() {
            @Override
            public void onStart(int i) {

            }

            @Override
            public void onSucceed(int i, Response<String> response) {


                parseUserInfok(response.get());

            }

            @Override
            public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
                MyUtils.Toast(getApplication(), "轨迹");
            }

            @Override
            public void onFinish(int i) {

            }
        });
    }

    private void parseUserInfok(String result) {

        zuohao=new ArrayList<ziti>();
        if (result != null) {
            dwlt=new ArrayList<dingwei>();
            LatLng ne=null;
            try {
                JSONArray array=new JSONArray(result);
                for(int i=0;i<array.length();i++){
                    JSONObject ll= (JSONObject) array.getJSONObject(i);
                    dingwei dw=new dingwei();
                    dw.setLongitude(ll.optString("Longitude"));
                    dw.setLatitude(ll.optString("Latitude"));
                    dw.setPhone(ll.optString("phone"));
                    dwlt.add(dw);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            if (dwlt.size()>0) {
                mBaiduMap.clear();
                List<LatLng> points = new ArrayList<LatLng>();
                for (int j = 0; j < dwlt.size(); j++) {
                    String lon = dwlt.get(j).getLongitude();
                    String lat = dwlt.get(j).getLatitude();

                    if (lon!= null&&lat!= null) {
                        try {
                            ne = new LatLng(Double.parseDouble(lon), Double.parseDouble(lat));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        ziti ziti = new ziti();
                        int k;
                        k=j+1;

                        ziti.mingzi = k + "号码" + dwlt.get(j).getPhone();
                        zuohao.add(ziti);
                        MyUtils.Toast(getApplication(), "轨迹" + dwlt.get(j).getPhone());
//                        Bundle nu=new Bundle();
//                        nu.putString("foo",j+"");
                        OverlayOptions optionll = new MarkerOptions().position(ne)
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_point));
                        mBaiduMap.addOverlay(optionll);
//                        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ne);
//                        mBaiduMap.setMapStatus(u);

                        points.add(ne);

                    }
                }
                if (points != null) {
                    OverlayOptions ooPolyline = new PolylineOptions().points(points);
                    mBaiduMap.addOverlay(ooPolyline);
                    LatLngBounds.Builder builder = new LatLngBounds.Builder();
                    for (LatLng p : points) {
                        builder = builder.include(p);
                    }
                    LatLngBounds latlngBounds = builder.build();
                    MapStatusUpdate u = MapStatusUpdateFactory.newLatLngBounds(latlngBounds, mMapView.getWidth(), mMapView.getHeight());
                    mBaiduMap.animateMapStatus(u);


                    MapStatus mMapStatus = new MapStatus.Builder()
                            .zoom(18.0f).build();
                    //   定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
                    MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
                            .newMapStatus(mMapStatus);
                    mBaiduMap.setMapStatus(mMapStatusUpdate);


                    MyUtils.Toast(getApplication(), "sas轨迹");
                    adapt adapt = new adapt(zuohao, getApplication());
                    list.setAdapter(adapt);
                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + dwlt.get(position).getPhone()));
                            startActivity(intent);
                        }
                    });

                }
            }

            }

        }
    private class ziti{
        private String  mingzi;
    }





}
